#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import uuid

from ManageDaemon.settings import *


def createActionSession(content):
    if ACTION_SESSION_PREFIX_NAME is None:
        Exception("ACTION_SESSION_PREFIX_NAME 配置为空,不能使用执行缓存")
    if ACTION_SESSION_FILE_PATH is None:
        Exception("ACTION_SESSION_FILE_PATH 配置为空,不能使用执行缓存")
    if not type(content) is dict:
        Exception("数据类型只能为dict")
    filename = str(uuid.uuid1())
    with open(ACTION_SESSION_FILE_PATH + "/" + ACTION_SESSION_PREFIX_NAME + filename, mode='w', encoding='utf-8') as f:
        json.dump(content, f)
    return filename


def updateActionSession(sessionid, content):
    if not type(content) is dict:
        Exception("数据类型只能为dict")
    with open(ACTION_SESSION_FILE_PATH + "/" + ACTION_SESSION_PREFIX_NAME + sessionid, mode='w', encoding='utf-8') as f:
        json.dump(content, f)

def hasActionSession(sessionid):
    fileName = ACTION_SESSION_FILE_PATH + "/" + ACTION_SESSION_PREFIX_NAME + sessionid
    return os.path.exists(fileName)

def readActionSession(sessionid):
    with open(ACTION_SESSION_FILE_PATH + "/" + ACTION_SESSION_PREFIX_NAME + sessionid, mode='r', encoding='utf-8') as f:
        content=json.load(f)
    return content
